    /*  
    championBuilder - Champions Online character planner.
    Copyright (C) 2009 Moritz Hartmeier
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You can find the GNU General Public License in the data folder.
    */


package ch.zarzu.champions.builder.gui;

import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.LinkedList;

import javax.swing.*;
import javax.swing.text.MaskFormatter;

import ch.zarzu.champions.builder.*;

public class CharacterSheet extends JLayeredPane implements KeyListener {
    MainInterface user_interface;
    SystemLink sys_link;
    PrefLink pref_link;
    
    TabPane tab_pane;
    LinkedList<JLayeredPane> panes;
    SpringLayout layout;
    JFormattedTextField name;
    private LinkedList<BaseCharacteristic> characteristics;
    
    private static CharacterSheet instance;
    
    private CharacterSheet() {
        user_interface = MainInterface.getInstance();
        sys_link = SystemLink.getInstance();
        pref_link = PrefLink.getInstance();
        
        layout = new SpringLayout();
        setLayout(layout);
        draw();
    }
    
    public void draw() {
        LinkedList<LinkedList<LinkedList<HashMap<String, String>>>> chars = sys_link.getCharacteristics();
        panes = new LinkedList<JLayeredPane>();
        
        name = new JFormattedTextField(createFormatter());
        name.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        name.setBackground(App.DARK_BLUE);
        name.setFont(new Font("Comic Sans MS", Font.BOLD, 13));
        name.setForeground(Color.WHITE);
        name.setCaretColor(Color.WHITE);
        name.addKeyListener(this);
        add(name);
        
        JLayeredPane pane;
        SpringLayout pane_layout;
        
        HashMap<String, String> tab_head;
        for(LinkedList<LinkedList<HashMap<String, String>>> tab_list : chars) {
            tab_head = tab_list.remove().getFirst();
            
            pane = new JLayeredPane();
            pane_layout = new SpringLayout();
            pane.setLayout(pane_layout);
            
            characteristics = new LinkedList<BaseCharacteristic>();
            HashMap<String, String> head;
            for(LinkedList<HashMap<String, String>> list : tab_list) {
                BaseCharacteristic characteristic;
                head = list.remove();
                if(!head.containsKey("formula"))
                    head.put("formula", "");
                if(!head.containsKey("concat"))
                    head.put("concat", "");
                if(!head.containsKey("unit"))
                    head.put("unit", "");
                characteristic = new BaseCharacteristic(head, list);
                characteristics.add(characteristic);
                pane.add(characteristic);
            }
            pane.setName(tab_head.get("name"));
            panes.add(pane);

            pane_layout.putConstraint(SpringLayout.EAST, characteristics.get(0), 0, SpringLayout.EAST, pane);
            pane_layout.putConstraint(SpringLayout.WEST, characteristics.get(0), 0, SpringLayout.WEST, pane);
            for(int i = 1; i < characteristics.size(); i++) {
                pane_layout.putConstraint(SpringLayout.EAST, characteristics.get(i), 0, SpringLayout.EAST, pane);
                pane_layout.putConstraint(SpringLayout.WEST, characteristics.get(i), 0, SpringLayout.WEST, pane);
                pane_layout.putConstraint(SpringLayout.NORTH, characteristics.get(i), 5, SpringLayout.SOUTH, characteristics.get(i - 1));
            }
            pane_layout.putConstraint(SpringLayout.SOUTH, pane, 0, SpringLayout.SOUTH, characteristics.get(characteristics.size() - 1));
        }
        tab_pane = new TabPane(panes);
        add(tab_pane);

        layout.putConstraint(SpringLayout.EAST, name, -10, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.SOUTH, name, 20, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.WEST, name, 60, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, name, 0, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.EAST, tab_pane, 0, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.NORTH, tab_pane, 20, SpringLayout.SOUTH, name);
        layout.putConstraint(SpringLayout.WEST, tab_pane, 0, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.SOUTH, this, 0, SpringLayout.SOUTH, tab_pane);
        
        name.setText("anon");
    }
    
    public String getCharacterName() {
        return name.getText().trim();
    }
    
    public void setCharacterName(String n) {
        name.setText(n);
    }
    
    protected MaskFormatter createFormatter() {
        MaskFormatter formatter = null;
        try{
            formatter = new MaskFormatter("******************************");
            formatter.setValidCharacters("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ \t");
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return formatter;
    }

    public void keyTyped(KeyEvent e) {
        if(e.getModifiers() != InputEvent.CTRL_MASK)
            user_interface.setChanged(true);
    }
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_TAB)
            System.out.println(".");//user_interface.nextPool();
            
    }
    public void keyReleased(KeyEvent e) {}
    
    public void updateLanguage() {
        tab_pane.updateLanguage();
    }
    
    public static CharacterSheet getInstance() {
        if(instance == null)
            instance = new CharacterSheet();
        return instance;
    }
}
